#include"Processbar.h"
#define Length 101
#define Style '#'
double bandwidth=1024*1024;
const char* lable="|/-\\";
//version 1
//void Processbar()
//{
//  char bar[Length];
//  memset(bar,'\0',sizeof(bar));
//  int cnt=0;
//  while(cnt<=100)
//  {
//    printf("[%-100s]\r",bar);
//    fflush(stdout);
//    bar[cnt]=Style;
//    cnt++;
//    usleep(200000);
//  }  
//  printf("\n");
//}

//version 2
void Processbar(double filesize,double current)
{
  char bar[Length];
  memset(bar,'\0',sizeof(bar));
  double rate=(current*100/filesize);
  int count=(int)rate;
  int cnt=0;
  int len=strlen(lable);
  while(cnt<=count)
  {
    bar[cnt]=Style;
    cnt++;
  }
    printf("[%-100s][%.1lf%%][%c]\r",bar,rate,lable[cnt%len]);
    fflush(stdout);
}
void download(double filesize,func cb)
{
  double current=0.0;
  while(current<=filesize)
  {
    Processbar(filesize,current);
    current+=bandwidth;
    usleep(20000);
  }
  printf("\n");
}
